package com.itm.ldap.dao.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.log4j.Logger;

import com.itm.ldap.dao.ContextoLDAPDao;
import com.itm.ldap.utils.Constantes;

public class ContextoLDAPDaoImpl implements ContextoLDAPDao{

	private final static Logger logger = Logger.getLogger(ContextoLDAPDaoImpl.class);
	private static Properties propiedadesLDAP;
	
	public ContextoLDAPDaoImpl() {	
		obtenerPropiedades();
	}
	
	public DirContext getContextoLDAPAdmin() {
		String serverIP = propiedadesLDAP.get(Constantes.LDAP_SERVERIP).toString();
		String baseName= propiedadesLDAP.get(Constantes.LDAP_NOMBRE_BASE_ADMIN).toString();
		String pwdAdmin = propiedadesLDAP.get(Constantes.LDAP_PWD_ADMIN).toString();
		DirContext ldapContext = null;
		
		try {
			// Set up environment for creating initial context
			Hashtable<String, String> ldapEnv = new Hashtable<String, String>();
			ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, Constantes.LDAP_CTX_FACTORY);
			ldapEnv.put(Context.PROVIDER_URL, serverIP);

			// Authenticate as S. User and password "mysecret"
			ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
			ldapEnv.put(Context.SECURITY_PRINCIPAL, baseName);
			ldapEnv.put(Context.SECURITY_CREDENTIALS, pwdAdmin);
			
			ldapContext = new InitialDirContext(ldapEnv);

		} catch (Exception e) {
			logger.error(" bind error: " + e.getMessage());
		}
		return ldapContext;
	}
	
	public DirContext getContextoLDAPUser(String userName, String userPwd) {
		String serverIP = propiedadesLDAP.get(Constantes.LDAP_SERVERIP).toString();
		String searchBaseName = propiedadesLDAP.get(Constantes.LDAP_NOMBRE_BASE).toString();
		String baseNameUsers= Constantes.LDAP_UID + userName + ',' + searchBaseName;
		DirContext ldapContext = null;
		
		try {
			// Set up environment for creating initial context
			Hashtable<String, String> ldapEnv = new Hashtable<String, String>();
			ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, Constantes.LDAP_CTX_FACTORY);
			ldapEnv.put(Context.PROVIDER_URL, serverIP);

			// Authenticate as S. User and password "mysecret"
			ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
			ldapEnv.put(Context.SECURITY_PRINCIPAL, baseNameUsers);
			ldapEnv.put(Context.SECURITY_CREDENTIALS, userPwd);
			
			ldapContext = new InitialDirContext(ldapEnv);

		} catch (Exception e) {
			logger.error(" bind error: " + e.getMessage());
		}
		return ldapContext;
	}
	
	public String getNombreBase() {
		return propiedadesLDAP.get(Constantes.LDAP_NOMBRE_BASE).toString();
	}
	
	private static void obtenerPropiedades() {
		try {
			String rutaPropiedades = Constantes.RUTA_CONF + "/" + Constantes.FICH_PROP_LDAP;
			propiedadesLDAP = new Properties();
			propiedadesLDAP.load(new FileInputStream(rutaPropiedades));
		} catch (FileNotFoundException e1) {
		} catch (IOException e) {}
	}
	
}
